//
//  WJTagSubViewController.m
//  BeDeJie
//
//  Created by jerry on 2017/1/27.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "WJSubTagViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <SDWebImage/SDImageCache.h>
#import <SVProgressHUD/SVProgressHUD.h>

#import "WJSubTagItem.h"
#import "WJSubTagCell.h"

static NSString *const ID = @"cell";

@interface WJSubTagViewController ()

@property (nonatomic, strong) NSArray *subTags;
@property (nonatomic,weak) AFHTTPSessionManager *mgr;

@end

@implementation WJSubTagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置背景色
    self.view.backgroundColor = WJColor(206, 206, 206);
    
    self.title = @"推荐标签";
    
    //加载数据
    [self loadData];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"WJSubTagCell" bundle:nil] forCellReuseIdentifier:ID];

    //分隔线处理步骤一：取消系统自带的分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //分隔线处理步骤二：把tableView背景色设置为分割线的背景色
    self.tableView.backgroundColor = WJColor(220, 220, 221);
    
    //提示用户
    [SVProgressHUD showWithStatus:@"正在加载..."];
}

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    //销毁提示器
    [SVProgressHUD dismiss];
    //取消之前的请求
    [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];
}

- (void)loadData {
    //1.创建请求会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    self.mgr = manager;
    
    //2.拼接参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"tag_recommend";
    params[@"action"] = @"sub";
    params[@"c"] = @"topic";
    
    //3.发送请求
    [manager GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:nil
         success:^(NSURLSessionDataTask *task, NSArray *responseObject) {
             [SVProgressHUD dismiss];
             
             // 字典转模型
             self.subTags = [WJSubTagItem mj_objectArrayWithKeyValuesArray:responseObject];
             //刷新表格
             [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];
        WJLog(@"请求sub tag错误");
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.subTags.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //自定义cell
    WJSubTagCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    //获取模型
    WJSubTagItem *item = self.subTags[indexPath.row];
    cell.item = item;
    
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
